package com.Cychat.post.controller;


import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.commons.entity.pojo.Post;
import com.Cychat.post.entity.dto.PostDTO;
import com.Cychat.post.entity.vo.PostVO;
import com.Cychat.post.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Auther: 崔航
 * @Date: 2025/10/28:14:31
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/api/post")
public class PostController {
    @Autowired
    private PostService postService;

    /**
     * 获取所有帖子
     * @return
     */
    @GetMapping("/getAllPosts/{pageNum}/{pageSize}")
    public ResultMessage getAllPosts(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
        List<PostDTO> allPosts = postService.getAllPosts(pageNum, pageSize);
        return ResultMessage.success(allPosts);
    }

    /**
     * 获取帖子详情
     * @param postId
     * @return
     */
    @GetMapping("/getPostById/{postId}/{userId}")
    public ResultMessage getPostById(@PathVariable("postId") Integer postId,@PathVariable("userId") Long userId) {
        PostDTO post = postService.getPostById(postId, userId);
        return ResultMessage.success(post);
    }
    /**
     * 添加帖子
     * @param post
     * @return
     */
    @PostMapping("/addPost/{userId}")
    public ResultMessage addPost(@RequestBody PostVO post, @PathVariable("userId") Long userId) {
        postService.addPost(post);
        return ResultMessage.success();
    }
    /**
     * 存oss
     * @param file
     * @return
     */
    @PostMapping("/updateAvatar/{userId}")
    public ResultMessage updateAvatar(@RequestParam("file") MultipartFile file, @PathVariable("userId") Long userId) throws IOException {
        //调用service层获取oss返回访问图片链接
        log.info("开始上传图片");
        String url = postService.updateAvatar(file);
        if (url!=null){
            return ResultMessage.success(url);
        }
        return ResultMessage.error("上传图片失败");
    }
    /**
     * 获取用户所有帖子
     * @param userId
     * @return
     */
    @GetMapping("/getPostByUserId/{userId}")
    public ResultMessage getPostByUserId(@PathVariable("userId") Long userId) {
        List<PostDTO> posts = postService.getPostByUserId(userId);
        return ResultMessage.success(posts);
    }
    /**
     * 删除帖子
     * @param postId
     * @return
     */
    @DeleteMapping("/deletePost/{postId}/{userId}")
    public ResultMessage deletePost(@PathVariable("postId") Integer postId, @PathVariable("userId") Long userId) {
        postService.removeById(postId);
        return ResultMessage.success();
    }
    /**
     * Description : 通过用户id查询发帖
     * ChangeLog : 1. 创建 (2025/11/6 15:56 [Esang]);
     * @param targetUserId 目标用户id
     * @return list<PostDTO> 发帖列表
    */
    @GetMapping("/getPostByTargetUserId/{targetUserId}/{userId}")
    public ResultMessage<List<PostDTO>> getPostByTargetUserId(@PathVariable("targetUserId") Long targetUserId,
                                                            @PathVariable("userId") Long userId) {
        return ResultMessage.success(postService.getPostByUserId(targetUserId));
    }
    /**
     * Description : 点赞
     * ChangeLog : 1. 创建 (2025/11/6 15:56 [Esang]);
     * @param postId 帖子id
     * @return PostDTO 帖子
    */
    @PutMapping("/like/{postId}/{userId}")
    public ResultMessage<PostDTO> like(@PathVariable("postId") Integer postId,@PathVariable("userId") Long userId) {
        return ResultMessage.success(postService.like(postId));
    }

    @GetMapping("/getPost")
    public ResultMessage getPost() {
        List<PostDTO> post = postService.getPost();
        return ResultMessage.success(post);
    }
    @DeleteMapping("/deletePostById/{postId}")
    public ResultMessage deletePostById(@PathVariable("postId") Integer postId) {
        postService.removeById(postId);
        return ResultMessage.success("1");
    }
}
